#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct examinee {
    char examno[10];      // 准考证号
    char name[10];        // 姓名
    char sex;             // 性别
    int age;            // 年龄
    char examtype[5];     // 考试类型
} ElemType;

typedef struct ListNode {
    ElemType data;
    struct ListNode *next;
} ListNode;

// 初始化链表
ListNode* initList() {
    ListNode *head = (ListNode*)malloc(sizeof(ListNode));
    head->next = NULL;
    return head;
}

// 输入考生信息
void inputCandidate(ElemType *candidate) {
    printf("请输入考生准考证号: ");
    scanf("%s", candidate->examno);
    printf("请输入考生姓名: ");
    scanf("%s", candidate->name);
    printf("请输入考生性别 (M/F): ");
    scanf(" %c", &candidate->sex);
    printf("请输入考生年龄: ");
    scanf("%d", &candidate->age);
    printf("请输入考试类型: ");
    scanf("%s", candidate->examtype);
    
}


// 插入考生信息
void insertCandidate(ListNode *head, ElemType candidate) {
    ListNode *newNode = (ListNode*)malloc(sizeof(ListNode));
    newNode->data = candidate;
    newNode->next = head->next;
    head->next = newNode;
    
    printf("考生信息插入成功\n");
    system("pause");
    system("cls");

}

// 输出所有考生信息
void outputCandidates(ListNode *head) {
    ListNode *current = head->next;
    while (current != NULL) {
        printf("准考证号: %s, 姓名: %s, 性别: %c, 年龄: %d, 考试类型: %s\n",
               current->data.examno, current->data.name, current->data.sex, current->data.age, current->data.examtype);
        current = current->next;
    }
    
    if (current == NULL)
        printf("当前没有考生信息\n");

    system("pause");
    system("cls");
   
}

// 查询考生信息
void queryCandidate(ListNode *head) {
    char examno[10];
    printf("请输入要查询的考生准考证号: ");
    scanf("%s", examno);

    ListNode *current = head->next;
    while (current != NULL) {
        if (strcmp(current->data.examno, examno) == 0) {
            printf("准考证号: %s, 姓名: %s, 性别: %c, 年龄: %d, 考试类型: %s\n",
                   current->data.examno, current->data.name, current->data.sex, current->data.age, current->data.examtype);
            
            system("pause");
            system("cls");
            return;
        }
        current = current->next;
        
    }
    printf("未找到该考生信息\n");
    
    system("pause");
    system("cls");

}

// 修改考生信息
void modifyCandidate(ListNode *head) {
    char examno[10];
    printf("请输入要修改的考生准考证号: ");
    scanf("%s", examno);

    ListNode *current = head->next;
    while (current != NULL) {
        if (strcmp(current->data.examno, examno) == 0) {
            printf("当前信息: \n");
            printf("准考证号: %s, 姓名: %s, 性别: %c, 年龄: %d, 考试类型: %s\n",
                   current->data.examno, current->data.name, current->data.sex, current->data.age, current->data.examtype);
            inputCandidate(&current->data);
            printf("考生信息修改成功\n");

            system("pause");
            system("cls");
            return;
        }
        current = current->next;
        
     }
    printf("未找到该考生信息\n");

    system("pause");
    system("cls");

}

// 删除考生信息
void deleteCandidate(ListNode *head) {
    char examno[10];
    printf("请输入要删除的考生准考证号: ");
    scanf("%s", examno);

    ListNode *current = head->next;
    ListNode *prev = head;
    while (current != NULL) {
        if (strcmp(current->data.examno, examno) == 0) {
            prev->next = current->next;
            free(current);
            printf("考生信息删除成功\n");
            
            system("pause");
            system("cls");
            
            return;
        }
        prev = current;
        current = current->next;

    }
    printf("未找到该考生信息\n");

    system("pause");
    system("cls");

}

int main() {
    ListNode *head = initList();
    int choice;

    while (1) {
        printf("\n考试报名管理系统\n");
        printf("1. 输入考生信息\n");
        printf("2. 输出考生信息\n");
        printf("3. 查询考生信息\n");
        printf("4. 修改考生信息\n");
        printf("5. 删除考生信息\n");
        printf("0. 退出\n");
        printf("请选择操作: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1: {
                ElemType candidate;
                inputCandidate(&candidate);
                insertCandidate(head, candidate);
                break;
            }
            case 2:
                outputCandidates(head);
                break;
            case 3:
                queryCandidate(head);
                break;
            case 4:
                modifyCandidate(head);
                break;
            case 5:
                deleteCandidate(head);
                break;
            case 0:
                return 0;
            default:
                printf("无效的选择，请重新输入\n");
        }
    }

    return 0;
}